from openpyxl import load_workbook
from openpyxl.drawing.image import Image


def insertimg2excel(imgPath, excelPath):
    imgsize = (720 / 4, 1280 / 4)  # 设置一个图像缩小的比例
    wb = load_workbook(excelPath)
    ws = wb.active
    ws.column_dimensions['A'].width = imgsize[0] * 0.14  # 修改列A的宽

    img = Image(imgPath)  # 缩放图片
    img.width, img.height = imgsize

    ws.add_image(img, 'A1')  # 图片 插入 A1 的位置上
    ws.row_dimensions[1].height = imgsize[1] * 0.78  # 修改列第1行的宽

    wb.save('out.xlsx')  # 新的结果保存输出


if __name__ == '__main__':
    img_url_0 = "https://support.content.office.net/en-us/media/2d9e717a-0077-438f-8e5e-f85a1305d4ad.jpg"

    imgPath = img_url_0
    excelPath = 'test.xlsx'
    insertimg2excel(imgPath, excelPath)
